import { payment, receipt, cancel } from "@/api";
import {
  Component,
  Vue
} from "vue-property-decorator";
@Component({})
export default  class OrderMix extends Vue {
  /**
   * 付款
   * @param {string} id 订单id
   */
  pay({ id, type },callback:()=>void) {
    payment(id, type === "BALANCE")
      .then(res => {
        if (type === "BALANCE") {
          uni.showToast({
            icon:"none",
            title:"支付成功"
          })
          if (callback) {
            callback();
          }
          return;
        }
        res = res.wxResult;
        uni.requestPayment({
          timeStamp: res.timeStamp,
          nonceStr: res.nonceStr,
          package: res.packageValue,
          signType: res.signType,
          paySign: res.paySign,
          success: () => {
            if (callback) {
              callback();
            }
            uni.showToast({
              icon:"none",
              title:"支付成功"
            })
          },
          fail: e => {
            const isCancel = msg => msg.includes("cancel");
            if (!isCancel(e.errMsg)) {
              uni.showToast({
                icon:"none",
                title:"支付失败"
              })
            }
          },
        });
      })
      .catch(err => {
        uni.showToast({
          icon:"none",
          title:`${err || '支付失败'}`
        })
      });
  }
  /**
   * 取消订单
   * @param {string} id 订单id
   */
   cancel({ id },callback:()=>void) {
    this.$Popup.modal({
      title: "温馨提示",
      content: "好不容易选好，确认要取消吗？"
    })
      .then(() => {
        cancel(id)
          .then(() => {
            this.$Popup.toast("取消成功")
            // Toast("取消成功");
            if (callback) {
              callback();
            }
          })
          .catch(err => {
            this.$Popup.toast(err || "操作失败")
          });
      })
  }

  /**
   * 确认收货
   * @param {string} id 订单id
   */
  receipt({ id, disabled },callback:()=>void) {
    if (disabled) return;
    this.$Popup.modal({
      title:"",
      content:"确认收货该订单？"
    }).then(() => {
      receipt(id)
        .then(() => {
          if (callback) {
            callback();
          }
          uni.showToast({
            icon:"none",
            title:"收货成功",
            duration:1000,
            complete:function(){
              uni.navigateTo({
                url: `/subcontract/pages/orderEvaluation/orderEvaluation?id=${id}`,
              });
            }
          })
        })
        .catch(err => {
          uni.showToast({
            icon:"none",
            title:`${err || '收货失败'}`
          })
        });
    })
    .catch(() => {
      uni.showToast({
        icon:"none",
        title:'收货失败'
      })
    });
  }
  /** 前往评价 */
  goReviews({
    target: {
      dataset: { id },
    },
  }) {
    uni.navigateTo({
      url: `/subcontract/pages/orderEvaluation/orderEvaluation?id=${id}`,
    });
  }
  /**
   * 操作按钮是否不可用
   * @param afs
   */
  receiptDisabled(afs) {
    // 换货状态
    if (
      ["POINT_EXCHANGE", "EXCHANGE"].indexOf(afs.type) !== -1 &&
      [
        "WAIT_FOR_SEND",
        "WAIT_FOR_PICKUP",
        "WAIT_RECEIPT",
        "WAIT_FOR_BUSINESS_RECEIPT",
      ].indexOf(afs.status) !== -1
    ) {
      return true;
    }
    return false;
  }

  getHasAfsSendOrder(status) {
    return ["WAIT_FOR_SEND"].indexOf(status) !== -1;
  }
  
  /** mixins内成功回调 */
  callbackAction() {
    this.$emit("call")
  }
};
